(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:PhpresourcesCtrl
     * @description
     * # PhpresourcesCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('PhpresourcesCtrl', PhpresourcesCtrl);

    PhpresourcesCtrl.$inject = ['$scope', 'dataService', 'helper', '$state', '$location', 'sessionService', '$timeout'];

    function PhpresourcesCtrl($scope, dataService, helper, $state, $location, sessionService, $timeout) {

        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);
        }

        $scope.getList = function(pageNo) {
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.pageSize,
            };

            if ($scope.controller) {
                query.controller = $scope.controller;
            }

            dataService.phpResourcesGet(query).then(function(list) {
                if (list) {
                    $scope.resource_list = list.resource_list;
                    $scope.total_count = list.total_count;
                }
            });
        }
        //全选
        $scope.checkAll =function(){
            $scope.xzmodel = angular.copy($scope.resource_list);
        }

        $scope.nocheckAll =function(){
            if($scope.xzmodel.length){
                $scope.xzmodel = [];
            }else{
                $scope.xzmodel = angular.copy($scope.resource_list);
            }
        }
        //批量保存
        $scope.batchSaveData =function(){
            console.error($scope.xzmodel);
            async.mapSeries($scope.xzmodel,function(data,call){
                console.error("data",data);
                saveOneResource(data).then(function(r) {
                    call(null,null);
                });
            },function(e,r){
                helper.message("修改成功！");
            });
        }
        //修改状态
        $scope.saveStatu = function(data, pid, type) {
            var savedata = { "pid": pid };
            savedata[type] = data;
            saveOneResource(JSON.parse(angular.toJson(savedata))).then(function(r) {
                if (r) {
                    helper.message("修改成功！");
                    $scope.getList(1);
                }
            });
        }

        //新增接口调用者
        $scope.addAction = function() {
            $scope.pid = $location.search().pid;
            if ($scope.pid) {
                var data = { "pid": $scope.pid };
                dataService.phpResourcesGet(data).then(function(r) {
                    if (r) {
                        $scope.resource_data = r.resource_list[0];
                    }
                })
            }
        }

        $scope.saveData = function() {
            saveOneResource($scope.resource_data).then(function(r) {
                if (r) {
                    helper.myAlert("success", null, null, function() {
                        $state.go("phpresources.list", null, {
                            reload: true
                        });
                    });
                }
            })
        }

        function saveOneResource(one) {
            return dataService.phpResourcesUpdate(one).then(function(result) {
                if (result.Msg != "ok") {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                } else {
                    return true;
                }
            });
        }

        //status=1启用，0不启用
        $scope.StatusUpdate = function(status, pid, list, $event) {
            var data = { "pid": pid, "status": status }
            dataService.phpResourcesStatusUpdate(data).then(function(r) {
                if (r.Msg == "ok") {
                    helper.myTip("success", $event.currentTarget);
                    $timeout(function() {
                        list.status = status;
                    });
                } else {
                    helper.myAlert("error：" + r.Msg);
                    return false;
                }
            });
        }
    }
})();